Skip to content

Conversation

@emekaokoli19
Copy link
Contributor

Description

This PR improves how RandomVariable copies RNG state when inplace=False.

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

@ricardoV94 I think this closes the #1708 using the code you provided, I just added some tests

@ricardoV94
Copy link
Member

Also make use of the new helper here:

new_rng = deepcopy(rng)

@ricardoV94 ricardoV94 changed the title enhanced RV RNG performance on deepcopy Speedup RNG copy in non-mutable RVs Nov 16, 2025
@emekaokoli19
Copy link
Contributor Author

@ricardoV94 Thank you for the checks. I have made the corrections

Comment on lines 361 to 369
@pytest.mark.performance
def test_custom_rng_deepcopy_faster_than_deepcopy():
rng = np.random.default_rng()

t_dp = timeit.timeit(lambda: deepcopy(rng), number=2000)
t_fc = timeit.timeit(lambda: custom_rng_deepcopy(rng), number=2000)

# Fast copy should be at least 20% faster
assert t_fc < t_dp * 0.8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need, and we have a different framework for benchmarking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I didn't know about the benchmarking framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Faster deepcopy of RNG in RandomVariables

2 participants